package org.mistfx.mvp.demo;


import com.sun.javafx.application.LauncherImpl;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.mistfx.mvp.demo.dashboard.DashboardView;
import org.mistfx.mvp.injection.Injector;

import java.time.LocalDate;
import java.time.Month;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Misty
 */
public class App extends Application {

    @Override
    public void init() throws Exception {
        /*
         * Properties of any type can be easily injected.
         */
        LocalDate date = LocalDate.of(4242, Month.JULY, 21);
        Map<Object, Object> customProperties = new HashMap<>();
        customProperties.put("date", date);
        /*
         * any function which accepts an Object as key and returns
         * and return an Object as result can be used as source.
         */
        Injector.setConfigurationSource(customProperties::get);

        System.setProperty("happyEnding", " Enjoy the flight!");
    }

    @Override
    public void start(Stage stage) throws Exception {
        DashboardView appView = new DashboardView();
        appView.getViewAsync(parent -> {
            System.out.println("show stage " + Thread.currentThread());
            Scene scene = new Scene(parent);
            stage.setTitle("mistFX-mvp demo");
            final String uri = getClass().getResource("app.css").toExternalForm();
            scene.getStylesheets().add(uri);
            stage.setScene(scene);
            stage.show();

            notifyPreloader(new StartedPreloaderNotification());
        });
    }

    @Override
    public void stop() throws Exception {
        Injector.forgetAll();
    }

    public static void main(String[] args) {
        LauncherImpl.launchApplication(App.class, MyPreloader.class, args);
    }
}
